//
//  BZThreadDemo.m
//  MZBasic
//
//  Created by Sings on 2023/7/6.
//

#import "BZThreadDemo.h"
#import "BZThreadQueue.h"
#import "BZThreadManager.h"

@implementation BZThreadDemo

- (void) startThreadDemo {
    
    BZThreadQueue *queue = [[BZThreadQueue alloc] init];
    queue.strLabel = @"Demo";
    queue.iConcurrency = 3;
    [queue addTask:[self getDemoTaskWithName:@"Demo1" AndTaskIndex:1 andTimeConsuming:20]];
    [queue addTask:[self getDemoTaskWithName:@"Demo1" AndTaskIndex:2 andTimeConsuming:10]];
    [queue addTask:[self getDemoTaskWithName:@"Demo1" AndTaskIndex:3 andTimeConsuming:5]];
    queue.finished = ^(BOOL Success) {
        NSLog(@"---Demo1 Finished");
    };
    [[BZThreadManager sharedInstance] addThreadQueue:queue];
    
    
    BZThreadQueue *queue1 = [[BZThreadQueue alloc] init];
    queue1.strLabel = @"Demo";
    queue1.iConcurrency = 3;
    [queue1 addTask:[self getDemoTaskWithName:@"Demo2" AndTaskIndex:1 andTimeConsuming:20]];
    [queue1 addTask:[self getDemoTaskWithName:@"Demo2" AndTaskIndex:2 andTimeConsuming:10]];
    [queue1 addTask:[self getDemoTaskWithName:@"Demo2" AndTaskIndex:3 andTimeConsuming:5]];
    queue1.finished = ^(BOOL Success) {
        NSLog(@"---Demo2 Finished");
    };
    [[BZThreadManager sharedInstance] addThreadQueue:queue1];
    
    
    BZThreadQueue *queue2 = [[BZThreadQueue alloc] init];
    queue2.strLabel = @"Demo";
    queue2.iConcurrency = 3;
    [queue2 addTask:[self getDemoTaskWithName:@"Demo3" AndTaskIndex:1 andTimeConsuming:20]];
    [queue2 addTask:[self getDemoTaskWithName:@"Demo3" AndTaskIndex:2 andTimeConsuming:10]];
    [queue2 addTask:[self getDemoTaskWithName:@"Demo3" AndTaskIndex:3 andTimeConsuming:5]];
    queue2.finished = ^(BOOL Success) {
        NSLog(@"---Demo3 Finished");
    };
    [[BZThreadManager sharedInstance] addThreadQueue:queue2];

    
    BZThreadQueue *queue3 = [[BZThreadQueue alloc] init];
    queue3.strLabel = @"Demo";
    queue3.iConcurrency = 3;
    [queue3 addTask:[self getDemoTaskWithName:@"Demo4" AndTaskIndex:1 andTimeConsuming:20]];
    [queue3 addTask:[self getDemoTaskWithName:@"Demo4" AndTaskIndex:2 andTimeConsuming:10]];
    [queue3 addTask:[self getDemoTaskWithName:@"Demo4" AndTaskIndex:3 andTimeConsuming:5]];
    queue3.finished = ^(BOOL Success) {
        NSLog(@"---Demo4 Finished");
    };
    [[BZThreadManager sharedInstance] addThreadQueue:queue3];
}

- (BZThreadQueueTask *) getDemoTaskWithName:(NSString *) name AndTaskIndex:(int) index andTimeConsuming:(int) time {
    BZThreadQueueTask *task = [[BZThreadQueueTask alloc] initWithAction:^(BZThreadQueueTaskFinishedBlock  _Nonnull finished) {
        NSLog(@"%@", [NSString stringWithFormat:@"---队列: %@,任务编号:%d 开始任务",name,index]);
        for (int i = 0; i < time; i++) {
            sleep(1);
        }
        NSLog(@"%@", [NSString stringWithFormat:@"---队列: %@,任务编号:%d 结束任务",name,index]);
        finished(YES);
    }];
    return task;
}

@end
